﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.baseinfo;

namespace EZCRS.HCMS.Web.manage.hotel
{
    public partial class salepolicy_add : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            btnsave.Text = Resources.Site.ButtonSave;

            if (!IsPostBack)
            {
                CFun.SetListControl(ddlClass, Bllbase_info.getbaseinfo(baseinfotype.价格代码类别, base.LanguageId), "");
                CFun.SetListControl(ddlType, Bllbase_info.getbaseinfo(baseinfotype.价格代码种类, LanguageId), "");
                int id = CFun.RequestNumber("id");

                InitPage();

                if (id > 0)
                {
                    HCRS_RateCode item = new BllHCRS_HotelLocal().getRateCode(id);

                    if (item != null)
                    {
                        //CFun.bindpagedata<h_RateCode>(this, item);
                        BindData(item);

                        btnsave.Text = Resources.Site.ButtonModify;
                        //btndel.Visible = true;
                        //btndel.Attributes.Add("onclick", "return confirm('" + Resources.Site.SystemConfirmDelete + "');");
                    }
                }
            }
            
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitPage()
        {
            Bllh_roomcode bl = new Bllh_roomcode();

            BllHCRS_Cxlrule cxlrule = new BllHCRS_Cxlrule();
            BllH_guarule guarule = new BllH_guarule();

            string strLan = base.LanguageId.ToString();

            List<HCRS_RoomCode> lst = bl.getitem("pid = " + base.BasPropertyId + " AND avaliable = 1 and languagetype="+base.LanguageId.ToString());

            foreach (HCRS_RoomCode room in lst)
                cblRooms.Items.Add(new ListItem(room.roomname, room.roomcode));


            List<HCRS_CxlRule> lstCxl = cxlrule.getitem("Property_id =" + base.BasPropertyId + " AND languagetype =" + strLan);

            foreach (HCRS_CxlRule cxl in lstCxl)
                ddlCxlRule.Items.Add(new ListItem(cxl.Rule, cxl.Rule));

            List<HCRS_GuaRule> lstGua = guarule.getitem("Property_id =" + base.BasPropertyId + " AND languagetype =" + strLan);

            foreach (HCRS_GuaRule g in lstGua)
                ddlGuarule.Items.Add(new ListItem(g.Grule, g.Grule));

            rblStatus.SelectedIndex = 0;



            EZCRS.HCMS.Bll.AIR.TAChannel channel = new EZCRS.HCMS.Bll.AIR.TAChannel();

            List<EZCRS.HCMS.Entity.Channel> lstChannel = channel.GetAll();

            foreach (EZCRS.HCMS.Entity.Channel c in lstChannel)
            {
                ListItem li = new ListItem();

                li.Text = c.Name;
                li.Value = c.ID.ToString();

                cblChannel.Items.Add(li);
            }

        }


        private void BindData(HCRS_RateCode item)
        {
            txtRate.Text = item.RateCode;
            txtName.Text = item.Name;

            txtDesc.Text = item.Description;

            txtBfAmount.Text = "0";
            txtBfPrice.Text = "0";

            if (item.State == 1)
                rblStatus.SelectedIndex = 0;
            else
                rblStatus.SelectedIndex = 1;

            ddlType.SelectedValue = item.RPI;
            ddlClass.SelectedValue = item.RateCat;

            ddlCxlRule.SelectedValue = item.CancelRule;
            ddlGuarule.SelectedValue = item.GuaranteeRule;

            CheckBox1.Checked = item.Basis;

            ddlCurrency.SelectedValue = item.Currency;

            BllRatePlan plan = new BllRatePlan();

            string strLan = base.CurrentLanguage;

            if (base.CurrentLanguage == "zh-CN")
                strLan = "0";
            else strLan = "1";

            List<HCRS_Plan> lst = plan.getPlanByRateCode(base.BasPropertyId, item.RateCode, Convert.ToInt16(strLan), 1);

            foreach(HCRS_Plan obj in lst)
            {
                foreach (ListItem li in cblRooms.Items)
                {
                    if (li.Value == obj.RoomCode)
                    {
                        li.Selected = true;
                        break;
                    }
                }
            }

            RateCode rc = new RateCode();

            List<HCRS_RATE_CODE_CHANNEL> lstChl = rc.GetChannelListByRateID(item.ID);

            foreach (ListItem li in cblChannel.Items)
            {
                foreach (HCRS_RATE_CODE_CHANNEL cl in lstChl)
                {
                    if (cl.ChannelID == int.Parse(li.Value))
                    {
                        li.Selected = true;
                        break;
                    }
                }
            }

        }


        protected void btnsave_Click(object sender, EventArgs e)
        {
            //写入数据库

            List<Channel> lstChannels = new List<Channel>();

            foreach (ListItem li in cblChannel.Items)
            {
                if (li.Selected)
                {
                    Channel cl = new Channel();

                    cl.ID = int.Parse(li.Value);
                    cl.Name = li.Text;

                    lstChannels.Add(cl);

                }
            }

            if (lstChannels.Count == 0)
            {
                Response.Write(CFun.AlertMeg(Resources.Site.NoChannelSelected, "back", ""));

                return;
            }


            HCRS_RateCode ratecode = new HCRS_RateCode();
            int id = CFun.RequestNumber("id");

            if(id > 0)
                ratecode.ID = id;

            ratecode.State = Convert.ToInt16(rblStatus.SelectedValue);

            ratecode.Name = txtName.Text.Trim();
            ratecode.Pid = base.BasPropertyId;
            ratecode.RateCat = ddlClass.SelectedValue;
            ratecode.RPI = ddlType.SelectedValue;
            ratecode.RateCode = txtRate.Text;
            ratecode.CancelRule = ddlCxlRule.SelectedValue;
            ratecode.GuaranteeRule = ddlGuarule.SelectedValue;
            ratecode.Description = txtDesc.Text;

            ratecode.updatetime = DateTime.Now;

            ratecode.Basis = CheckBox1.Checked;

            ratecode.Currency = ddlCurrency.SelectedValue;


            RateCode ratecodeBll = new RateCode();

            //检测是否重复
            if (ratecodeBll.IsExist(txtRate.Text, id, base.BasPropertyId))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.RateCodeRepeat, "back", ""));

                return;
            }

            Dictionary<string, bool> lstRoom = new Dictionary<string, bool>();

            foreach (ListItem li in cblRooms.Items)
                lstRoom.Add(li.Value, li.Selected);

            string strLan = base.LanguageId.ToString();

            int iBack = ratecodeBll.Save(ratecode, lstChannels, lstRoom, Convert.ToInt16(strLan));

            if (iBack == 0)
                Response.Redirect("salepolicy.aspx");
            else
                Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
        }

        protected void btndel_Click(object sender, EventArgs e)
        {
            //int id = CFun.RequestNumber("id");
            //if(true)
            //    Response.Write(CFun.AlertMeg(Resources.Site.SystemDeleteSuccess, "", "salepolicy.aspx"));
            //else
            //    Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
        }
    }
}
